// pages/releaseResume/releaseResume.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [],
    typeListValue: '',
    educationStatus: [{
      value: '学生',
      checked: false
    }, {
      value: '已毕业',
      checked: false
    }],
    avatar: '../../images/touxiang.png',
    sex: [{
      value: '男',
      checked: false
    }, {
      value: '女',
      checked: false
    }],
    region: [],
    health: ['健康', '良好', '其他'],
    healthIndex: null,
    birthDate: '',
    habitList: [],
    habitValues: [],
    studyStatus: ['优秀', '良好', '其他'],
    studyIndex: null,
    myEducation: ['博士', '硕士', '本科', '专科', '高中'],
    myEducationIndex: null,
    workType: [{
      value: '短期'
    }, {
      value: '长期'
    }, {
      value: '周末'
    }],
    workMode: [{
      value: '线上'
    }, {
      value: '线下'
    }],
    workTypeValue: '',
    workTime: [{
        value: '工作日'
      }, {
        value: '周末'
      },
      {
        value: '节假日'
      }, {
        value: '寒暑假'
      },
    ],
    workTimeValue: '',
    startTime: [
      ['01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00',
        '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00',
      ],
      ['01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00',
        '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00',
      ]
    ],
    timeIndex: [0, 0],
    certificateImg: [],
    resumeContent: {},
    r_args25: [],
    userResume: {}
  },

  // 业务类型
  typeChange: function (e) {
    let items = e.detail.value
    let typeList = this.data.typeList
    for (let i = 0; i < typeList.length; i++) {
      typeList[i].checked = false
      for (let j = 0; j < items.length; j++) {
        if (items.length > 6) {
          items.splice(0, 1)
          if (typeList[i].wt_name == items[j]) {
            let key = `typeList[${i}].checked`
            this.setData({
              [key]: true
            })
          }
        } else {
          if (typeList[i].wt_name == items[j]) {
            let key = `typeList[${i}].checked`
            this.setData({
              [key]: true
            })
          }
        }
      }
    }
    this.setData({
      typeList
    })
    this.data.typeListValue = items.toString()
  },

  // 教育状态 radio
  educationChange: function (e) {
    const educationStatus = this.data.educationStatus
    for (let i = 0; i < educationStatus.length; i++) {
      educationStatus[i].checked = educationStatus[i].value === e.detail.value
    }
    this.setData({
      educationStatus
    })
  },

  // 上传头像
  uploadAvatar: function () {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0]
        that.setData({
          avatar: tempFilePaths
        })
      }
    })
  },

  // 性别
  sexChange: function (e) {
    const sex = this.data.sex
    for (let i = 0; i < sex.length; i++) {
      sex[i].checked = sex[i].value === e.detail.value
    }
    this.setData({
      sex
    })
  },

  // 省市区
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  // 出生日期
  birthChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      birthDate: e.detail.value
    })
  },

  // 健康状况
  healthChange: function (e) {
    this.setData({
      healthIndex: e.detail.value
    })
  },

  // 生活习惯
  habitChange: function (e) {
    const habitList = this.data.habitList
    const values = e.detail.value
    console.log(values)
    for (let i = 0, lenI = habitList.length; i < lenI; ++i) {
      habitList[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (habitList[i].value === values[j]) {
          habitList[i].checked = true
          break
        }
      }
    }
    this.setData({
      habitList,
      habitValues: values
    })
  },

  // 教育状态 picker
  studyChange: function (e) {
    this.setData({
      studyIndex: e.detail.value
    })
  },

  // 学历
  myEducationChange: function (e) {
    this.setData({
      myEducationIndex: e.detail.value
    })
  },

  // 工作类型
  workTypeChange: function (e) {
    const workType = this.data.workType
    for (let i = 0; i < workType.length; i++) {
      workType[i].checked = workType[i].value === e.detail.value
    }
    this.setData({
      workType,
      workTypeValue: e.detail.value
    })
  },

  // 工作方式
  workModeChange: function (e) {
    const workMode = this.data.workMode
    for (let i = 0; i < workMode.length; i++) {
      workMode[i].checked = workMode[i].value === e.detail.value
    }
    this.setData({
      workMode,
      workTimeValue: e.detail.value
    })
  },

  // 工作时间
  workTimeChange: function (e) {
    const workTime = this.data.workTime
    for (let i = 0; i < workTime.length; i++) {
      workTime[i].checked = workTime[i].value === e.detail.value
    }
    this.setData({
      workTime
    })
  },

  // 上班时段
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let arr = e.detail.value
    this.setData({
      timeIndex: e.detail.value
    })

  },

  bindTimeColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
  },

  // 上传证书
  uploadCertificate: function (e) {
    let that = this
    let certificateImg = that.data.certificateImg
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0]
        certificateImg.push(tempFilePaths)
        that.setData({
          certificateImg
        })
        wx.uploadFile({
          url: 'https://wx.zhonghuazhengxin.com/api/Upload/pic',
          filePath: tempFilePaths,
          name: 'file',
          formData: null,
          success: res => {
            let imgSrc = JSON.parse(res.data)
            that.data.r_args25.push(imgSrc.result_data)
          }
        })
      }
    })
  },

  // 删除证书
  deleteImg: function (e) {
    let certificateImg = this.data.certificateImg
    let index = e.target.dataset.index
    certificateImg.splice(index, 1)
    this.data.r_args25.splice(index, 1)
    this.setData({
      certificateImg
    })
  },

  // 预览证书
  previewImage: function (e) {
    let current = e.target.dataset.src
    let certificateImg = this.data.certificateImg

    wx.previewImage({
      urls: certificateImg,
      current
    })
  },


  // 提交简历
  improveResume: function (e) {
    let that = this
    let obj = e.detail.value
    obj.token = wx.getStorageSync('token')
    obj.r_args25 = that.data.r_args25.toString().replace(/,/g, '|')
    that.data.resumeContent.r_args2 = that.data.typeListValue
    that.data.resumeContent.r_args6 = that.data.region[0]
    that.data.resumeContent.r_args7 = that.data.region[1]
    that.data.resumeContent.r_args8 = that.data.region[2]
    that.data.resumeContent.r_args9 = that.data.birthDate
    that.data.resumeContent.r_args11 = that.data.health[that.data.healthIndex]
    that.data.resumeContent.r_args12 = that.data.habitValues.toString()
    that.data.resumeContent.r_args14 = that.data.myEducation[that.data.myEducationIndex]
    that.data.resumeContent.r_args21 = that.data.workTypeValue
    that.data.resumeContent.r_args22 = that.data.workTimeValue
    that.data.resumeContent.r_args24 = `${that.data.startTime[0][that.data.timeIndex[0]]}-${that.data.startTime[1][that.data.timeIndex[1]]}`
    that.data.sex.map(item => {
      if (item.checked) {
        that.data.resumeContent.r_args5 = item.value
      }
    })
    obj = Object.assign(obj, that.data.resumeContent)
    console.table(obj);
    if (obj.r_args1 == '' || obj.r_args15 == '' || obj.r_args16 == '' || obj.r_args18 == '' || obj.r_args25 == '' || obj.r_args5 == '' || obj.r_args9 == '' || obj.r_args11 == '' || obj.r_args12 == '' || obj.r_args14 == '' || obj.r_args2 == '' || obj.r_args19 == '' || obj.r_args20 == '' || obj.r_args21 == '' || obj.r_args22 == '' || obj.r_args24 == '' || obj.r_args26 == '' || obj.r_args27 == '' || obj.r_args17 == '') {
      wx.showToast({
        title: '请完善资料',
        icon: 'none'
      })
    } else {
      app.post('Resume/save', obj).then(res => {
        wx.showModal({
          title: '提示',
          content: '提交成功,请等待审核',
          showCancel: false,
          success: res => {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let district = wx.getStorageSync('district')
    let city = wx.getStorageSync('city')
    let province = wx.getStorageSync('province')
    this.setData({
      region: [province, city, district]
    })
    wx.showLoading({
      title: '正在加载...',
    })
    app.post('Common/workType').then(res => {
      let typeList = res.data.result_data
      typeList = jsonUniq(typeList, 'wt_name')
      typeList.map(item => {
        item.checked = false
      })
      that.setData({
        typeList
      })
    }).then(() => {
      app.post('Common/resumeHabit').then(res => {
        let habitList = res.data.result_data
        let list = []
        habitList.map(item => {
          list.push({
            value: item
          })
        })
        that.setData({
          habitList: list
        })
      }).then(() => {
        app.post('Resume/resume', {
          token: wx.getStorageSync('token')
        }).then(res => {
          let obj = res.data.result_data
          obj.r_args12 = obj.r_args12.split(',')
          obj.r_args2 = obj.r_args2.split(',')
          obj.r_args24 = obj.r_args24.split('-')
          console.log(obj);

          that.data.sex.map((item, index) => {
            if (item.value == obj.r_args5) {
              item.checked = true
            }
          })

          that.data.health.map((item, index) => {
            if (item == obj.r_args11) {
              that.data.healthIndex = index
            }
          })

          for (let i = 0; i < that.data.habitList.length; i++) {
            for (let j = 0; j < obj.r_args12.length; j++) {
              if (that.data.habitList[i].value == obj.r_args12[j]) {
                that.data.habitList[i].checked = true
              }
            }
          }

          that.data.myEducation.map((item, index) => {
            if (item == obj.r_args14) {
              that.data.myEducationIndex = index
            }
          })

          for (let i = 0; i < that.data.typeList.length; i++) {
            for (let j = 0; j < obj.r_args2.length; j++) {
              if (that.data.typeList[i].wt_name == obj.r_args2[j]) {
                that.data.typeList[i].checked = true
              }
            }
          }

          that.data.workType.map((item, index) => {
            if (item.value == obj.r_args21) {
              item.checked = true
            }
          })

          that.data.workMode.map((item, index) => {
            if (item.value == obj.r_args22) {
              item.checked = true
            }
          })

          for (let i = 0; i < that.data.startTime.length; i++) {
            for (let j = 0; j < that.data.startTime[i].length; j++) {
              if (that.data.startTime[i][j] == obj.r_args24[0]) {
                that.data.timeIndex[0] = j
              }
              if (that.data.startTime[i][j] == obj.r_args24[1]) {
                that.data.timeIndex[1] = j
              }
            }
          }

          that.data.r_args25 = obj.r_args25
          that.data.typeListValue = obj.r_args2.toString()
          that.data.workTimeValue = obj.r_args21
          that.data.workTypeValue = obj.r_args22
          that.data.habitValues = obj.r_args12.toString()
          that.setData({
            userResume: obj,
            sex: that.data.sex,
            birthDate: obj.r_args9,
            healthIndex: that.data.healthIndex,
            habitList: that.data.habitList,
            myEducationIndex: that.data.myEducationIndex,
            typeList: that.data.typeList,
            workType: that.data.workType,
            workMode: that.data.workMode,
            timeIndex: that.data.timeIndex,
            certificateImg: obj.r_args25,
          })
        })
      })
    })



    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

//json数组去重
function jsonUniq(arr, key) { //json 数组去重

  let arr1 = [arr[0]];

  arr.forEach(function (item1, idx1) {

    let flag = false;

    arr1.forEach(function (item2, idx2) {

      if (item1[key] == item2[key]) {

        flag = true;

        return;

      }

    })

    if (!flag) {

      arr1.push(item1)

    }

  })

  return arr1;

}